package com.example.javaproject.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.javaproject.dto.SalaryDTO;
import com.example.javaproject.dto.SalaryStatsDTO;
import com.example.javaproject.entity.Salary;
import com.example.javaproject.mapper.SalaryMapper;
import com.example.javaproject.service.SalaryService;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class SalaryServiceImpl extends ServiceImpl<SalaryMapper, Salary> implements SalaryService {

    @Override
    public Page<SalaryDTO> getSalaryPage(Page<SalaryDTO> page, Integer status, String studentName, String teacherName, String positionName) {
        return baseMapper.selectSalaryPage(page, status, studentName, teacherName, positionName);
    }

    @Override
    public Page<SalaryDTO> getStudentSalaryPage(Page<SalaryDTO> page, Integer studentId) {
        return baseMapper.selectStudentSalaryPage(page, studentId);
    }

    @Override
    public boolean auditSalary(Integer id, Integer status) {
        Salary salary = this.getById(id);
        if (salary != null) {
            salary.setStatus(status);
            return this.updateById(salary);
        }
        return false;
    }

    @Override
    public Page<SalaryDTO> getTeacherSalaryPage(Page<SalaryDTO> page, Integer teacherId, Integer status, String studentName, String positionName) {
        return baseMapper.selectTeacherSalaryPage(page, teacherId, status, studentName, positionName);
    }

    @Override
    public boolean teacherApproveSalary(Integer salaryId, Integer teacherId) {
        Salary salary = this.getById(salaryId);
        if (salary == null) {
            return false;
        }
        if (!teacherId.equals(salary.getTeacherId())) {
            return false;
        }
        salary.setStatus(1); // 1-已发放
        salary.setUpdateTime(LocalDateTime.now());
        return this.updateById(salary);
    }

    @Override
    public boolean teacherRejectSalary(Integer salaryId, Integer teacherId) {
        Salary salary = this.getById(salaryId);
        if (salary == null) {
            return false;
        }
        if (!teacherId.equals(salary.getTeacherId())) {
            return false;
        }
        salary.setStatus(2); // 2-已拒绝
        salary.setUpdateTime(LocalDateTime.now());
        return this.updateById(salary);
    }

    @Override
    public boolean teacherResendSalary(Integer salaryId, Integer teacherId) {
        Salary salary = this.getById(salaryId);
        if (salary == null) {
            return false;
        }
        if (!teacherId.equals(salary.getTeacherId())) {
            return false;
        }
        salary.setStatus(0); // 0-待发放
        salary.setUpdateTime(LocalDateTime.now());
        return this.updateById(salary);
    }

    @Override
    public boolean teacherUpdateSalary(Integer salaryId, Integer teacherId, Integer totalHour, BigDecimal totalSalary) {
        Salary salary = this.getById(salaryId);
        if (salary == null) {
            return false;
        }
        if (!teacherId.equals(salary.getTeacherId())) {
            return false;
        }
        // 只能修改未发放状态的工资记录
        if (salary.getStatus() != 2) {
            return false;
        }
        
        // 验证数据
        if (totalHour == null || totalHour < 0 || totalHour > 127) {
            return false; // TINYINT限制
        }
        if (totalSalary == null || totalSalary.compareTo(BigDecimal.ZERO) < 0) {
            return false;
        }
        
        salary.setTotalHour(totalHour);
        salary.setTotalSalary(totalSalary);
        salary.setUpdateTime(LocalDateTime.now());
        return this.updateById(salary);
    }

    @Override
    public List<SalaryStatsDTO> getSalaryStats() {
        return baseMapper.getSalaryStats();
    }
}